<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/13
 * Time: 9:16
 */
namespace app\reserve\controller;

use think\Controller;
use think\Db;
use think\Request;
use app\reserve\model\AdminAction as ActionModel;

class Adminaction extends Controller{

    private $action;

    public function __construct()
    {
        $this->action = new ActionModel();
    }

    /**
     * 添加权限
     * User: lijie
     * Date: 2018/10/13
     */
    public function addAction(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,'AdminAction.addAction');
        if ($vali !== true){
            return apiResponse(201,$vali,'');
        }
        // 判断父级id是否存在
        if (empty($param['fid'])){
            $param['fid'] = 0;
        }
        // 添加
        $res = $this->action->addAction($param);
        if ($res){
            return apiResponse(200,'添加成功',$res);
        }else {
            return apiResponse(201,'添加失败',$res);
        }
    }

    /**
     * 删除权限
     * User: lijie
     * Date: 2018/10/13
     */
    public function delAction()
    {
        $action_id = input('post.action_id');
        if (empty($action_id)){
            return apiResponse(201,'权限id不能为空','');
        }
        //
        $res = $this->action->delAction($action_id);
        if ($res){
            return apiResponse(200,'删除成功',$res);
        }else {
            return apiResponse(201,'删除失败',$res);
        }
    }

    /**
     * 编辑权限
     * User: lijie
     * Date: 2018/10/13
     */
    public function editAction(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,"AdminAction.editAction");
        if ($vali !== true){
            return apiResponse(201,$vali,'');
        }
        $where['action_id'] = $param['action_id'];
        $where['status']    = array("NEQ",9);
        $res = $this->action->editAction($param,$where);
        if ($res){
            return apiResponse(200,'编辑成功',$res);
        }else {
            return apiResponse(201,'编辑失败',$res);
        }
    }

    /**
     * 权限列表
     * User: lijie
     * Date: 2018/10/13
     */
    public function selectAction(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,"AdminAction.selectAction");
        if ($vali !== true){
            return apiResponse(201,$vali,'');
        }
        //
        $where['status']    = array("NEQ",9);
        $res['list']  = $this->action->actionList($where,$param['pagenum'],$param['listNum']);
        $res['total'] = $this->action->where($where)->count();
        if ($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,'获取失败',$res);
        }
    }

    /**
     * 权限列表二级分类
     * User: lijie
     * Date: 2018/10/13
     */
    public function actionList()
    {

        $where['fid'] = 0;
        $res = $this->action->selectAction($where);
        //
        $where2['status'] = array("NEQ",9);
        $where2['fid']    = array("NEQ",0);
        $res2 = $this->action->where($where2)->field("action_id as id,action_name as label,fid")->select();
        //
        foreach ($res as $key => $val) {   // 父级

            foreach ($res2 as $k => $v) {  // 子级

                if ($res2[$k]['fid'] == $res[$key]['id']){
                    $temp['id']       = $res2[$k]['id'];
                    $temp['label']       = $res2[$k]['label'];
                    $children[] = $temp;
                    unset($res2[$k]);
                    $res[$key]['children'] = $children;
                }
            }
            // 清空数组数据，避免数据叠加
            $children = array();
        }
        $data['list']   = $res;
        if ($res){
            return apiResponse(200,'获取成功',$data);
        }else {
            return apiResponse(201,'获取失败',$data);
        }
    }

    /**
     * 权限详情
     * User: lijie
     * Date: 2018/10/13
     */
    public function findAction()
    {
        $action_id = input('post.action_id');
        if (empty($action_id)){
            return apiResponse(201,'权限id不能为空','');
        }
        //
        $where['action_id'] = $action_id;
        $where['status']    = array("NEQ",9);
        $res = $this->action->findAction($where);
        if ($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,'获取失败',$res);
        }
    }

    /**
     * 权限详情二级分类
     * User: lijie
     * Date: 2018/10/15
     */
    public function actionInfo()
    {
        $action_id = input('post.action_id');
        if (empty($action_id)){
            return apiResponse(201,'权限id不能为空','');
        }
        $where['action_id'] = $action_id;
        $where['status']    = array("NEQ",9);
        $res['label'] = $this->action->findAction($where);
        // 子级
        $where_child['fid'] = $res['label']['action_id'];
        $where_child['status'] = array("NEQ",9);
        $res['label']['children'] = $this->action->where($where_child)->field("action_id,action_name,action_name_en,ctime,utime,status")->select();

        if ($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,'获取失败',$res);
        }
    }

    /**
     * 查询一级菜单
     * User: lijie
     * Date: 2018/10/13
     */
    public function oneMenu()
    {
        $where['fid']   = 0;
        $where['status'] = array("NEQ",9);
        //
        $res = $this->action->where($where)->order("ctime desc")->field("action_id,action_name")->select();
        if ($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,'获取失败',$res);
        }
    }
}